<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            list-style-type: none;
        }

        .container {
            width: 800px;
            height: 500px;
            margin: 100px auto;
            border: 1px solid #ccc;
        }

        .title {
            display: flex;
            align-items: center;
            background: #eee;
        }

        .title li {
            flex: 1;
            text-align: center;
            padding: 15px 0;
            cursor: pointer;
            color: #2b2b2b;
        }

        .title li.active {
            background: orangered;
            color: #fff;
        }

        .content {
            padding: 10px;
            box-sizing: border-box;
        }

        .content li {
            display: none;
        }

        .content li.active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul class="title">
            <li class="active">火车票</li>
            <li>汽车票</li>
            <li>电影票</li>
            <li>公园票</li>
            <li>飞机票</li>
        </ul>
        <ul class="content">
            <li class="active">11火车票火车票火车票火<h1>车票火车票火车票火车票</h1>火车票火车票火车<h1>车票火车票火车票火车票</h1>票火车票火车票</li>
            <li>22汽车票汽车票汽车票<h1>车票火车票火车票火车票</h1>汽车票汽车票<h1>车票火车票火车票火车票</h1>汽车票汽车票汽车票<h1>车票火车票火车票火车票</h1>汽车票汽车票汽车票汽车票汽车票
            </li>
            <li>33火车票火车票火车票火<h1>车票火车票火车票火车票</h1>火车票火车票火车<h1>车票火车票火车票火车票</h1>票火车票火车票</li>
            <li>44汽车票汽车票汽车票<h1>车票火车票火车票火车票</h1>汽车票汽车票<h1>车票火车票火车票火车票</h1>汽车票汽车票汽车票<h1>车票火车票火车票火车票</h1>汽车票汽车票汽车票汽车票汽车票
            </li>
            <li>55飞机票飞机票飞机票<h1>车票火车票火车票火车票</h1>飞机票飞机票飞机票飞机<h1>车票火车票火车票火车票</h1>票飞机票<h1>车票火车票火车票火车票</h1>
                飞机票飞机票飞机票飞机票飞机票飞机票飞机票飞机票飞机票飞机票飞机票</li>
        </ul>
    </div>

    <script>

        var titles = document.getElementsByClassName("title")[0].children
        var contents = document.getElementsByClassName("content")[0].children

        for (let i = 0; i < titles.length; i++) {
            //标题项被点击时触发
            titles[i].onclick = () => {
                clearActive()
                //切换新的active项
                titles[i].classList.add('active')
                contents[i].classList.add("active")
            }
        }

        var clearActive = () => {
            //清除当前active样式
            var activelist = document.getElementsByClassName("active")
            var length = activelist.length
            for (let j = length - 1; j >= 0; j--) {
                activelist[j].classList.remove('active')
            }
        }

    </script>
</body>

</html>